package com.wbs.service.impl;

import com.mysql.jdbc.StringUtils;
import com.wbs.mapper.TbAdminMapper;
import com.wbs.model.TbAdmin;
import com.wbs.model.TbAdminExample;
import com.wbs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;

import javax.swing.plaf.ListUI;
import java.util.List;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/1/26 15:46
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private TbAdminMapper tbAdminMapper;

    @Autowired
    public AdminServiceImpl(TbAdminMapper tbAdminMapper){
        this.tbAdminMapper = tbAdminMapper;
    }


    @Override
    public TbAdmin selectByUsername(String username) {
        TbAdmin tbAdmin = new TbAdmin();
        tbAdmin.setUsername(username);
        //使用条件查询方法
        List<TbAdmin> tbAdminList = this.conditionalExtraction(tbAdmin);

        return !ListUtils.isEmpty(tbAdminList)?tbAdminList.get(0):null;
    }


    //辅助方法 条件查询
    private List<TbAdmin> conditionalExtraction(TbAdmin tbAdmin) {
        TbAdminExample tbAdminExample = new TbAdminExample();
        TbAdminExample.Criteria criteria = tbAdminExample.createCriteria();
        //根据名字进行严格查询
        if (!StringUtils.isNullOrEmpty(tbAdmin.getUsername())){
            criteria.andUsernameEqualTo(tbAdmin.getUsername());
        }
        return tbAdminMapper.selectByExample(tbAdminExample);
    }
}
